package code;

/**
 * 
 * @author 
 *
 * This class is a helper class.
 * It contains static method to be called by other class
 */
public class Util {
	/**
	 * 
	 * @param position  the input position to check
	 * @throws LocationFormatException  when the input position's format is incorrect
	 */
	public static void checkPostionFormat(String position) throws LocationFormatException{
		if (position.length() == 6) {//when length is 6 then must start with s and followed by 5 digits
			if (!(position.charAt(0) == 's' || position.charAt(0) == 'S')) {
				throw new LocationFormatException("Wrong current location format!");
			}
			for (int i = 1; i < 6; i++) {
				char tc = position.charAt(i);
				if (!(tc >= '0' && tc <= '9')) {
					throw new LocationFormatException("Wrong current location format!");
				}
			}
		} else if (position.length() == 4) {//when length is 4 then must start with c and followed by 3 digits
			if (!(position.charAt(0) == 'c' || position.charAt(0) == 'C')) {
				throw new LocationFormatException("Wrong current location format!");
			}
			for (int i = 1; i < 4; i++) {
				char tc = position.charAt(i);
				if (!(tc >= '0' && tc <= '9')) {
					throw new LocationFormatException("Wrong current location format!");
				}
			}
		} else if (position.length() == 3) {// then length is 3 it can only be out
			if (!position.equalsIgnoreCase("out")) {
				throw new LocationFormatException("Wrong current location format!");
			}
		} else {// else it is in wrong format
			throw new LocationFormatException("Wrong current location format!");
		}
	}
}
